package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.pinda.auth.entity.PdAuthUser;
import com.github.pagehelper.PageHelper;
import com.pinda.auth.entity.PdCoreOrg;
import com.pinda.auth.entity.PdCoreStation;
import com.pinda.auth.search.PdAuthUserSearch;

import com.pinda.auth.view.DriverView;
import com.pinda.auth.view.PasswordView;
import com.pinda.auth.view.PdAuthUserView;
import com.pinda.authorization.entity.User;
import com.pinda.base.entity.PdFleet;
import com.pinda.base.entity.PdTruckDriver;
import com.pinda.commonutils.Key;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;
import com.pinda.feign.PdDriverFenClient;
import com.pinda.mapper.*;
import com.pinda.service.IPdAuthUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@Service
public class PdAuthUserServiceImpl extends ServiceImpl<PdAuthUserMapper, PdAuthUser> implements IPdAuthUserService {

    @Resource
    private PdAuthUserMapper pdAuthUserMapper;
    @Resource
    private PdCoreOrgMapper pdCoreOrgMapper;
    @Resource
    private PdCoreStationMapper pdCoreStationMapper;
    @Resource
    private PdDriverFenClient pdDriverFenClient;

    @Override
    public PageInfo<PdAuthUserView> findUserService(Page<PdAuthUserSearch> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return new PageInfo<PdAuthUserView>(this.pdAuthUserMapper.findPageSearchAuthUser(page.getSearch()));
    }

    @Override
    public Result<DriverView> driverLogin(PdAuthUser user) {
        QueryWrapper<PdAuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", user.getAccount());
        if (pdAuthUserMapper.exists(queryWrapper)) {
            PdAuthUser pdAuthUser = pdAuthUserMapper.selectOne(queryWrapper);
            String pwd = Key.rsaDecryptByPrivate(pdAuthUser.getPassword());
            if (user.getPassword().equals(pwd)){
                if (pdAuthUser.getStatus() != 0) {
                    PdTruckDriver pdTruckDriver = pdDriverFenClient.selectDriver(pdAuthUser.getId());
                    PdCoreOrg pdCoreOrg = pdCoreOrgMapper.selectById(pdAuthUser.getOrgId());
                    PdFleet pdFleet = pdDriverFenClient.selectFleet(pdTruckDriver.getFleetId());
                    DriverView driverView = new DriverView();
                    driverView.setUserId(pdTruckDriver.getUserId());
                    driverView.setOrgName(pdCoreOrg.getName());
                    driverView.setName(pdAuthUser.getName());
                    driverView.setFleetName(pdFleet.getName());
                    driverView.setMobile(pdAuthUser.getMobile());
                    driverView.setAvatar(pdAuthUser.getAvatar());
                    return new Result<>(true, 200, "登录成功", driverView);
                } else {
                    return new Result<>(false, 300, "账号已被禁用，请联系管理员", null);
                }
            }
        }
        return new Result<>(false, 300, "用户名或密码错误", null);
    }

    @Override
    public int CheckPasswordorUpdatePasswordService(PasswordView passwordView) {
        PdAuthUser pdAuthUser = this.pdAuthUserMapper.selectById(passwordView.getId());
        if (!ObjectUtils.isEmpty(pdAuthUser)) {
            String s = Key.rsaDecryptByPrivate(pdAuthUser.getPassword());
            if (s.equals(passwordView.getOldpassword())) {
                String s1 = Key.rsaEncryptByPublic(passwordView.getNewpassword());
                PdAuthUser pdAuthUser1 = new PdAuthUser();
                pdAuthUser1.setPassword(s1);
                UpdateWrapper wrapper = new UpdateWrapper();
                wrapper.eq("id", passwordView.getId());
                this.pdAuthUserMapper.update(pdAuthUser1, wrapper);
            } else {
                return 2;
            }
        }
        return 1;
    }

    @Override
    public List<User> listByIdsUser(List<Long> ids) {
        return this.pdAuthUserMapper.listByIdsUser(ids);
    }

    @Override
    public List<PdCoreOrg> findPdCoreOrgService() {
        return this.pdCoreOrgMapper.selectList(null);
    }

    @Override
    public List<PdCoreStation> findCoreStationService() {
        return this.pdCoreStationMapper.selectList(null);
    }

}
